﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AnaViewer
{
    public partial class ThemeManagerForm : Form
    {
        Properties.Settings settings = Properties.Settings.Default;
        public ThemeManagerForm()
        {
            InitializeComponent();

            listBoxThemes.SelectedIndexChanged += new EventHandler(listBoxThemes_SelectedIndexChanged);

            tsbNew.Click += new EventHandler(tsbNew_Click);
            tsbEdit.Click += new EventHandler(tsbEdit_Click);
            tsbDelete.Click += new EventHandler(tsbDelete_Click);

            tsbApply.Click += new EventHandler(tsbApply_Click);


            settings.PropertyChanged += new PropertyChangedEventHandler(Default_PropertyChanged);
            labelThemeName.Text = string.Format("当前主题为“{0}”。", settings.AnaTheme);
        }

        void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ThemeName")
                labelThemeName.Text = string.Format("当前主题为“{0}”。", settings.AnaTheme);
        }

        void tsbApply_Click(object sender, EventArgs e)
        {
            if (listBoxThemes.SelectedItems.Count <= 0)
                return;

            settings.AnaTheme = listBoxThemes.SelectedItem as string;
            settings.Save();

        }
        void UpdateList()
        {
            string selected = listBoxThemes.SelectedItem as string;

            listBoxThemes.Items.Clear();
            foreach (string themeName in ThemeHelper.Default.SupportThemeNames)
                listBoxThemes.Items.Add(themeName);

            listBoxThemes.SelectedItem = selected;//尝试选中原来的选中项
            if(listBoxThemes.Items.Count > 0 && listBoxThemes.SelectedItems.Count <=0)
                listBoxThemes.SelectedIndex = 0;
        }

        

        void tsbDelete_Click(object sender, EventArgs e)
        {
            if (listBoxThemes.SelectedItems.Count <= 0)
                return;
            DialogResult result = MessageBox.Show(this.TopLevelControl, "该删除操作不可恢复，您确定要删除该主题吗？", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                ThemeHelper.Default.DeleteThemeData(listBoxThemes.SelectedItem as string);
                UpdateList();
            }
        }
        void tsbEdit_Click(object sender, EventArgs e)
        {
            if(listBoxThemes.SelectedItems.Count <=0)
                return;

            ThemeForm form = new ThemeForm();
            string themeName =listBoxThemes.SelectedItem as string;
            form.SetTheme(themeName,ThemeHelper.Default.GetThemeData(themeName));
            if (form.ShowDialog(this.TopLevelControl) == DialogResult.OK)
            {
                ThemeData theme = form.GetTheme();
                ThemeHelper.Default.SetThemeData(themeName, theme);
                anaControl1.UpdateTheme(ThemeHelper.Default.GetThemeData(themeName));

                listBoxThemes.SelectedItem = themeName;
            }
        }

        void tsbNew_Click(object sender, EventArgs e)
        {
            string themeName = "新建主题";
            NewThemeForm nameForm = new NewThemeForm();
            nameForm.ThemeName = themeName;
            if (nameForm.ShowDialog(this.TopLevelControl) != DialogResult.OK)
                return;
            themeName = nameForm.ThemeName;

            ThemeForm form = new ThemeForm();
            form.SetTheme(themeName, ThemeHelper.Default.GetThemeData(themeName));
            if (form.ShowDialog(this.TopLevelControl) == DialogResult.OK)
            {
                ThemeData theme = form.GetTheme();
                ThemeHelper.Default.SetThemeData(themeName, theme);
                
                UpdateList();
                listBoxThemes.SelectedItem = themeName;
            }
        }

        void listBoxThemes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxThemes.SelectedItems.Count <= 0)
            {
                tsbEdit.Enabled = false;
                tsbDelete.Enabled = false;

                tsbApply.Text = "应用主题(&A)";
                tsbApply.Enabled = false;
            }
            else
            {
                tsbEdit.Enabled = true;
                tsbDelete.Enabled = true;

                tsbApply.Text = string.Format("应用“{0}”主题(&A)",listBoxThemes.SelectedItem as string);
                tsbApply.Enabled = true;

                anaControl1.UpdateTheme(ThemeHelper.Default.GetThemeData(listBoxThemes.SelectedItem as string));
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            UpdateList();
            listBoxThemes_SelectedIndexChanged(this, null);

        }
    }
}